<template>
    <el-container>
      <el-header><div id="logo"><img src="../../../assets/logo.png"/></div></el-header>
      <el-main>
      <label>用户名</label>
      <el-input ref="username_input" v-model="username" ></el-input>
      <label>密码</label>
      <el-input  ref="passwd_input" type="password" auto-complete="off"></el-input>
      <label>验证码</label>
      <img ref="vcode_img" :src="captcha" @click="doRefresh"/>
      <el-input  ref="vcode_input" v-model="vcode"></el-input>
      <el-button ref="submit_btn" @click="doLogin" type="primary">提交</el-button>
      <el-button ref="register_btn" @click="doRegister">注册</el-button>
      <el-button ref="reset_btn" @click="doReset">重置</el-button>
      </el-main>
    </el-container>
</template>

<script>
    var baseUrl = "http://129.1.9.46:8726"
    export default {
        name: "Login",
        data: function () {
          return {
            isEdit: true,
            username: 'jimmymo',
            password: 'root1234',
            vcode: '',
            captcha: baseUrl+'/vcodes?scenario=login'
          }
        },
        methods: {
          doRegister(event) {
            if (event){
              this.$router.push("register")
            }
          },
          doRefresh(event) {
            this.captcha = baseUrl + '/vcodes?scenario=login&d='+Math.random();
          },
          doLogin(event) {
            if (event){
              this.isEdit = false
              this.$http.get(baseUrl+"/security/salts?username="+this.username).then(function(res){
                var salt = res.bodyText
                // alert(salt)
                var passport = hex_sha1(this.password+salt)
                alert(passport)
                this.$http.post(baseUrl+"/security/tokens?username="+this.username+"&passport="+passport+"&vcode="+this.vcode).then(function(res){
                  this.$message({message: '登入成功',type: 'success'});
                  var accessToken = res.body.accesstoken
                  // var permissions = res.body.permissions
                  // var roles = res.body.roles
                  window.sessionStorage.setItem('username', this.username)
                  window.sessionStorage.setItem('passport', passport)
                  window.sessionStorage.setItem('accessToken', accessToken)
                  this.$router.push("dashboard")
                }, function(err){
                  this.isEdit = true
                  this.$message({message: '登入失败',type: 'error'});
                  console.log(err)
                })

              }, function(err){
                console.log(err)
              })
            }
          },
          doReset(event) {
            if (event){
              // this.$refs.username_input.resetCurrentValue()
            }
            // this.$refs[formName].resetFields();
          }
        },
        created: function() {

        }
    }
</script>

<style scoped>

</style>
